package com.hotel_management_backstage.lin.controller.staffManagement;

import com.alibaba.fastjson.JSONObject;
import com.hotel_management_backstage.lin.entity.staffManagement.SysStaffShow;
import com.hotel_management_backstage.lin.result.RespBean;
import com.hotel_management_backstage.lin.service.staffManagement.SysStaffDepartmentService;
import com.hotel_management_backstage.lin.service.staffManagement.SysStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@RestController
public class staffController {
    @Autowired
    private SysStaffDepartmentService sysStaffDepartmentService;

    @Autowired
    private SysStaffService sysStaffService;

    @GetMapping("api/allStaffShow")
    public RespBean getAllStaffShow(){
        return RespBean.success("",sysStaffDepartmentService.selectAllStaffShow());
    }

    @GetMapping("api/getSysUserByCurrentStaff")
    public RespBean getSysUserByCurrentStaff(HttpServletRequest request){
        return RespBean.success("", sysStaffService.getSysStaffByCurrentUsername(request));
    }

    @PostMapping("api/insertStaff")
    public RespBean insertStaff(@RequestBody SysStaffShow sysStaffShow){
        sysStaffDepartmentService.insertStaff(sysStaffShow);
        return RespBean.success("操作成功");
    }

    @PostMapping("api/updateStaff")
    public RespBean updateStaff(@RequestBody SysStaffShow sysStaffShow){
        sysStaffDepartmentService.updateStaff(sysStaffShow);
        return RespBean.success("操作成功");
    }

    @PostMapping("api/deleteStaff")
    public RespBean deleteStaff(HttpServletRequest req) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
        StringBuffer sb = new StringBuffer();
        String s = null;
        while((s=br.readLine()) != null){
            sb.append(s);
        }
        JSONObject jsonObject = JSONObject.parseObject(sb.toString());
        Integer staffNumber = jsonObject.getInteger("staffNumber");
        sysStaffDepartmentService.deleteStaffByStaffNumber(staffNumber);
        return RespBean.success("操作成功");
    }
}
